package com.wanxi.service.impl;

import com.wanxi.model.Permission;
import com.wanxi.model.UserModel;
import com.wanxi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

/**
 * @Author:Doteon
 * @createDate:2023/3/11
 * @version: 1.0
 */
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {
    @Autowired
    UserService userService;

    @Override
    public UserModel loadUserByUsername(String username) throws UsernameNotFoundException {
        UserModel userModel=userService.getUserByName(username);
        List<Permission> permissionList=userService.getPermissionsByUserId(userModel.getId());
        HashSet<Permission> permissions=new HashSet<>(permissionList);
        userModel.setAuthorities(permissions);
        return userModel;
    }

}
